package com.cykj.eurekaclient.controller;


import com.alibaba.fastjson.JSON;
import com.cykj.eurekaclient.bean.FrontUser;
import com.cykj.eurekaclient.service.FrontUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @version 1.0
 * @author: zhiMing
 * @date: 2021/9/24 17:00
 * @desc: 处理有关前端用户登录请求的控制层
 */
@RestController
public class FrontUserController {

    @Autowired
    FrontUserService frontUserService;

    /**
     * @version 1.0
     * @author: zhiMing
     * @date: 2021/9/24 17:00
     * @desc: 处理有关前端用户登录请求,将请求提交给service层
     */
    @RequestMapping("/frontUserLogin")
    public Map<String,Object> frontUserLogin(@RequestBody Map<String,String> data){
        FrontUser frontUser = null;
        if (data.get("frontUser")!= null && !data.get("frontUser").equals("")){
            frontUser = JSON.parseObject(data.get("frontUser"),FrontUser.class);
            System.out.println(JSON.toJSONString(frontUser));
        }
        Map<String,Object> map = frontUserService.frontUserLogin(frontUser,data.get("code"));
        return map;
    }

    /**
     * @version 1.0
     * @author: zhiming
     * @date: 2021/9/27 00:14
     * @desc: 获取乘客上传的头像数据
     */
    @RequestMapping("/updateFrontUserAvatar")
    public String updateFrontUserAvatar(@RequestBody Map<String,String> data){
        String fileContent = data.get("fileContent");
        String fileUrl = data.get("fileUrl");
        String fileSuffix = data.get("fileSuffix");
        FrontUser frontUser = JSON.parseObject(data.get("userInfo"),FrontUser.class);
        boolean conBool = (fileContent!=null && !fileContent.equals(""));
        boolean urlBool = (fileUrl != null && !fileUrl.equals(""));
        if (conBool || urlBool){
           if (conBool){
               frontUser = frontUserService.updateFrontUserAvatar(frontUser,fileContent,fileSuffix);

           }
           if (urlBool&&!conBool){
               frontUser = frontUserService.updateFrontUserAvatarUrl(frontUser,fileUrl,fileSuffix);
           }
            if (frontUser!=null){
                return JSON.toJSONString(frontUser);
            }
        }
        return "-200";
    }
}
